function nulval(srcValue, defaultValue){
    return srcValue == null ? defaultValue : srcValue;
}

function find_or_create_tab_content(title, function_url_prefix, page_code, icon) {
    var itemId = CONTENT_PANEL_PREFIX + page_code;
    var rst = top.contentPanel.getComponent(itemId);
    if (rst != null) {
        rst.show();
    } else {
//        var panel = Ext.create('Ext.panel.Panel',
//            {
//                itemId: itemId,
//                title: title,
//                margins: '0 0 0 0',
//                autoScroll: false,
//                closable: true,
//                html: '<iframe style="overflow:auto;width:100%;height:100%;" frameborder="0"  src="' + function_url_prefix + tab_id + '"></iframe>'
//            });
        var iconCfg = {};
        if(icon){
            iconCfg['icon'] = icon;
        } else {
            iconCfg['iconCls'] = 'x-tree-icon-leaf';
        }

        var panelCfg = Ext.apply({
            itemId: itemId,
            iframe_url: function_url_prefix + page_code,
            title: title
        }, iconCfg);

        var panel = Ext.create("jwawa.erp.framework.IframePanel", panelCfg);
        top.contentPanel.add(panel).show();
    }
}

function find_or_create_win(shellClassName,  winCfg, shellCfg) {
    var itemId = CONTENT_WIN_PREFIX + shellClassName;
    var win = Ext.ComponentQuery.query('window[itemId=' + itemId + ']')[0];
    var config;

    if (win != null) {
        return win;
    }

    if(!shellCfg){
        shellCfg = {}
    }
    var shell = Ext.create(shellClassName, shellCfg);

    var tmpWinCfg = {
        itemId: itemId,
        title : shell.title,
        height:  nulval(shell.height, 300),
        width: nulval(shell.width, 500),
        resizable: nulval(shell.resizable, true),
        closable: nulval(shell.closeable, true),
        closeAction: 'hide',
        minimizable: nulval(shell.minimizable, false),
        maximizable: nulval(shell.maximizable, false),
        icon: nulval(shell.icon, false),
        layout: {
            type: 'fit'
        }
    };

    shell.closeable = false;

    if (winCfg != null) {
        Ext.applyIf(winCfg, tmpWinCfg);
        config = winCfg;
    } else {
        config = tmpWinCfg;
    }

    win = Ext.create("Ext.window.Window", config);
    win.addListener('close', function(){
        shell.fireEvent('containerClose', win);
    });

    win.add(shell);

    return win;
}

function showWindow(win, adjustPos){
    if(adjustPos === undefined){
        adjustPos = true;
    }

    win.show();
    if(adjustPos){
        win.alignTo(Ext.getBody(), "t-t", [0, 20]);
    }
}

function alertError(message, callbackFn, adjustPos){
    if(callbackFn == null){
        callbackFn = function(){};
    }

    if(adjustPos === undefined){
        adjustPos = true;
    }

    var win = Ext.Msg.show({
        title: '错误',
        msg: Ext.util.Format.htmlEncode(message),
        buttons: Ext.MessageBox.OK,
        icon: Ext.MessageBox.ERROR,
        fn: callbackFn
    });

    if(adjustPos){
        win.alignTo(Ext.getBody(), "c-c", [0, -100]);
    }

    return win;
}

function alertInfo(message, adjustPos){
    if(adjustPos === undefined){
        adjustPos = true;
    }

    var win = Ext.Msg.show({
        title: '提示',
        msg: Ext.util.Format.htmlEncode(message),
        buttons: Ext.MessageBox.OK,
        icon: Ext.MessageBox.INFO
    });

    if(adjustPos){
        win.alignTo(Ext.getBody(), "c-c", [0, -100]);
    }

    return win;
}

function alertConfirm(title, message, fn, adjustPos){
    if(adjustPos === undefined){
        adjustPos = true;
    }

    var win = Ext.Msg.confirm(title,
        message,
        fn
    );

    if(adjustPos){
        win.alignTo(Ext.getBody(), "c-c", [0, -100]);
    }

    return win;
}

function convertRawStrToJasn(cfgStr){
   return Ext.decode(Ext.isEmpty(cfgStr) ? '{}' : cfgStr);
}

function fieldKeyNavBind(formPanel, submitFn, owner){
    var me = this;

    if(Ext.isString(formPanel)){
        formPanel = Ext.ComponentQuery.query(formPanel)[0];
    }

    new Ext.util.KeyMap({
        target: formPanel.getEl(),
        key: Ext.EventObject.ENTER,
        ctrl: false,
        shift: false,
        defaultEventAction: "stopEvent",
        scope: me,
        fn: function(key, event){
            var el = Ext.FocusManager.focusedCmp;
            if(el.kbSubmit){
                el.blur();
                submitFn.call(owner);
            }else if (el.nextSibling('field[kbNav=true]')) {
                el.nextSibling('field[kbNav=true]').focus();
            }
        }
    });
}

function getEflLabel(entityName, fieldName){
    if(Ext.isEmpty(entityName)){
        return null;
    }

    var eflMapping = Ext.JwawaResMgr.get(EFL_MAPPING);
    if(eflMapping === null || eflMapping === undefined){
        return null;
    }

    var entity = eflMapping[entityName];
    if(entity != null && entity != undefined){
       var fieldLabel = eflMapping[entityName][fieldName];
       return Ext.isEmpty(fieldLabel) ? null : fieldLabel;
    } else {
       return null;
    }
}